// pages2/pages/index/cameraChannelList/index.js
const $api = require('../../../../request/api').API;
import Toast from '@vant/weapp/toast/toast';
import { IMOU_APPID, IMOU_APPSECRET, IMOU_TOKEN } from '../../../../utils/storage';
const app = getApp();
Page({
  data: {
    deviceId: '',
    deviceInfo: {},
    channelList: []
  },
  onLoad({ deviceId }) {
    this.setData({
      deviceId: deviceId
    });
    this.getToken();
  },
  /**
   * 获取设备
   */
  async getDevice() {
    const { result } = await $api.listDeviceDetailsByIds({ deviceList: [{ deviceId: this.data.deviceId }] });
    const deviceInfo = (result.data?.deviceList || [])[0];
    const channelList = deviceInfo.channelList || [];
    channelList.forEach(item => {
      const { lastOffLineTime } = item;
      const year = lastOffLineTime.slice(0, 4);
      const month = lastOffLineTime.slice(4, 6);
      const day = lastOffLineTime.slice(6, 8);
      const hours = lastOffLineTime.slice(9, 11);
      const minutes = lastOffLineTime.slice(11, 13);
      const seconds = lastOffLineTime.slice(13, 15);
      item.lastOffLineTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    });
    this.setData({ deviceInfo, channelList });
  },
  /**
   * 登录获取token
   */
  async getToken() {
    const params = {
      appId: IMOU_APPID,
      appSecret: IMOU_APPSECRET
    };
    Toast.loading('数据加载中');
    const { result } = await $api.getImouToken(params);
    app.globalData[IMOU_TOKEN] = result.data.accessToken;
    await this.getDevice();
    Toast.clear();
  }
});
